package com.boesch.electroOffice.business.birt;

import java.io.File;
import java.io.Serializable;
import java.util.Date;

import com.boesch.electroOffice.business.businessObject.ReportBusinessObject;
import com.boesch.electroOffice.business.common.BusinessException;
import com.boesch.electroOffice.model.birt.ReportOptions;
import com.boesch.electroOffice.utils.FileUtils;
import com.boesch.electroOffice.utils.PortalUtils;
import com.boesch.electroOffice.utils.PortletUtils;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;

public class BerichtResource implements Serializable {
	
	private static final long serialVersionUID = 8640576639079189178L;
	private static final Log log = LogFactoryUtil.getLog(BerichtResource.class);
	
	private static final String WEBAPPS = "webapps/";
	private static final String REPORTS = "reports/";
	
	private ReportBusinessObject reportBusinessObject;
	private String reportRelativePath;
	private String reportAbsolutePath;
	private Date lastModified;
	private ReportOptions options;
	private String[] destinationFileName;


	public BerichtResource(ReportOptions options, ReportBusinessObject reportBusinessObject) {
		if (null != options && null != reportBusinessObject) {
			this.options = options;
			this.reportBusinessObject = reportBusinessObject;
			cleanFoldersInResourcePath();
			setBerichtDirection();
			log.debug("Bericht Resource initialized...");
		}
	}

	private void cleanFoldersInResourcePath() {
		log.debug("Start cleaning old report folders");
		Date date = new Date();
		long time = date.getTime();
		File file = new File(PortalUtils.getPortalRealPath("") + WEBAPPS + REPORTS);
		File[] files = file.listFiles();
		if (null != files) {
			handleFilesInResourcePath(time, files);
		}
	}

	/**
	 * @param time
	 * @param files
	 * @throws NumberFormatException
	 */
	private void handleFilesInResourcePath(long time, File[] files) throws NumberFormatException {
		for (File tempFile : files) {
			if (tempFile.isDirectory()) {
				long folderTime = Long.parseLong(tempFile.getName());
				//10800000 Millisekunden entsprechen 3 Stunden
				if ((time - folderTime) > 10800000) {
					FileUtils.removeDirectory(tempFile);
					log.debug("Deleted directory " + tempFile);
				}
			}
		}
	}

	private void setBerichtDirection() {
		destinationFileName = options.getReportName().split("\\.");
		lastModified = new Date();
		
		String filePath = String.valueOf(lastModified.getTime())
				+ "/" + PortletUtils.getPortletSession().getId();
		
		reportRelativePath = REPORTS + filePath;
		reportAbsolutePath = PortalUtils.getPortalRealPath("") + WEBAPPS + reportRelativePath;
	}

	public String generateReport() throws BusinessException {
		File pathResource = new File(reportAbsolutePath);
		log.debug("AbsolutePath of File pathResource"
				+ pathResource.getAbsolutePath());
		if (!pathResource.exists()) {
			pathResource.mkdirs();
		}
		// Check if absolute pfad in ReportName
		if (options.getReportName().contains("\\")
				|| options.getReportName().contains("/")) {
			File file = new File(options.getReportName());
			options.setReportName(file.getName());
		}
		destinationFileName = options.getReportName().split("\\.");
		String filePath = destinationFileName[0] 
				+ "(" + String.valueOf(lastModified.getTime()) + ")."
				+ options.getOutFormat();
		reportBusinessObject.doRenderReport(options, reportAbsolutePath + "/" + filePath);
		log.debug("Absolute file path: " + reportAbsolutePath);
		log.debug("Relative file path: " + reportRelativePath);
		return "/" + reportRelativePath + "/" + filePath;
	}
}
